from sqlalchemy import Column, DateTime, Integer, String, BigInteger, Boolean, CHAR, func
from base import Base
from sqlalchemy.orm import relationship

class SysRole(Base):
    __tablename__ = 'sys_roles'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='角色ID')
    role_name = Column(String(30), nullable=False, comment='角色名称')
    role_key = Column(String(100), nullable=False, comment='角色权限字符串')
    role_sort = Column(Integer, nullable=False, comment='显示顺序')
    data_scope = Column(CHAR(1), default='1', comment='数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）')
    menu_check_strictly = Column(Boolean, default=True, comment='菜单树选择项是否关联显示')
    dept_check_strictly = Column(Boolean, default=True, comment='部门树选择项是否关联显示')
    status = Column(CHAR(1), nullable=False, comment='角色状态（0正常 1停用）')
    del_flag = Column(CHAR(1), default='0', comment='删除标志（0代表存在 2代表删除）')
    create_by = Column(String(64), default='', comment='创建者')
    created_at = Column(DateTime, default=func.now(), comment='创建时间')
    update_by = Column(String(64), default='', comment='更新者')
    updated_at = Column(DateTime, onupdate=func.now(), comment='更新时间')
    deleted_at = Column(DateTime, nullable=True, comment='删除时间')
    remark = Column(String(500), nullable=True, comment='备注')

    depts = relationship('SysDept', secondary='sys_role_depts', back_populates='roles')
    admin_menu = relationship('AdminMenu', back_populates='role_menus')

    def __repr__(self):
        return f"<SysRole(role_id={self.id}, role_name={self.role_name})>"
